package codeine.mail;

import java.util.List;

import codeine.configuration.ConfiguredProjectUtils;
import codeine.configuration.HttpCollector;
import codeine.configuration.MailPolicy;
import codeine.jsons.nodes.NodeJson;
import codeine.jsons.project.ProjectJson;
import codeine.jsons.project_status.ProjectStatusList;

public class ShouldSendMailValidator {
	private HttpCollector collector;
	private ConfiguredProjectUtils configuredProjectUtils;
	private boolean result;
	private boolean previousResult;
	private ProjectJson project;
	private NodeJson node;
	private ProjectStatusList projectStatusList;

	public ShouldSendMailValidator(boolean result, boolean previousResult, HttpCollector collector,	ConfiguredProjectUtils configuredProjectUtils, ProjectJson project, NodeJson node, ProjectStatusList projectStatusList) {
		this.result = result;
		this.previousResult = previousResult;
		this.collector = collector;
		this.configuredProjectUtils = configuredProjectUtils;
		this.project = project;
		this.node = node;
		this.projectStatusList = projectStatusList;
	}

	private boolean shouldMailByPolicies(List<MailPolicy> policies) {
		for (MailPolicy p : policies) {
			if (p.isActive(previousResult, result)) {
				return true;
			}
		}
		return false;
	}

	private boolean shouldMailByDependencies() {
		for (HttpCollector master : configuredProjectUtils.dependsOn(collector, project)) {
			String resultOfMaster = projectStatusList.project_name_to_status.get(project.name()).monitor_to_status().get(node.name()).get(master.name());
			if ("false".equals(resultOfMaster)) {
				return false;
			}
		}
		return true;
	}

	public boolean shouldMail() {
		throw new UnsupportedOperationException();
//		List<MailPolicy> calculatedPolicies = project.mailingPolicy();
//		for (CollectorRule rule : collector.rule) // overwrite mailing policies if there are explicit rules
//		{
//			if (rule.mailingPolicy != null && rule.shouldApplyForNode(node.name)) {
//				calculatedPolicies = rule.mailingPolicy;
//			}
//		}
//		return calculatedPolicies.contains(MailPolicy.EachRun)
//				|| (shouldMailByPolicies(calculatedPolicies) && shouldMailByDependencies());
	}
}
